package com.thundersoft.pickcolor.application;

import android.app.Application;
import android.content.ContentValues;
import android.net.Uri;
import android.os.StrictMode;

import com.thundersoft.pickcolor.utils.ShareUtils;
import com.thundersoft.pickcolor.utils.StaticClass;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import es.dmoral.toasty.Toasty;

public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        cameraProblem();
        initConfig();
        if(isFirst()){
            init();
        }
    }

    private void cameraProblem() {
        // 解决相机报错问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    private void initConfig() {
        Toasty.Config.getInstance()
        .tintIcon(true)
        .setTextSize(20)
        .allowQueue(true)
        .apply();
    }

    // 初始化数据库
    private void init() {
        // 添加数据
        Uri uriColor = Uri.parse("content://com.thundersoft.pickcolor.provider/color");
        ContentValues values = new ContentValues();
        readerJson(uriColor,values);
    }

    // 判断程序是否第一次运行
    private boolean isFirst() {
        boolean isFirst = ShareUtils.getBoolean(this,StaticClass.SHARE_IS_FIRST,
                true);
        if(isFirst){
            // 是第一次运行
            ShareUtils.putBoolean(this,StaticClass.SHARE_IS_FIRST,false);
            return true;
        }else {
            return false;
        }
    }

    // 读取json文件，保存到数据库中
    private void readerJson(Uri uriColor, ContentValues values){
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(getAssets().open("data/colordata.json"),
                    "UTF-8");
            br = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while((line = br.readLine()) != null){
                builder.append(line);
            }
            JSONObject testjson = new JSONObject(builder.toString());// builder读取了JSON中的数据。

            // 直接传入JSONObject来构造一个实例
            JSONArray array = testjson.getJSONArray("color");// 从JSONObject中取出数组对象
            for (int i = 0; i < array.length(); i++) {
                JSONObject color = array.getJSONObject(i);// 取出数组中的对象
                values.clear();

                // 取出数组中对象的各个值
                values.put("hexValue",color.getString("hexValue"));
                values.put("rValue",color.getString("rValue"));
                values.put("gValue",color.getString("gValue"));
                values.put("bValue",color.getString("bValue"));
                values.put("kind",color.getString("kind"));
                values.put("describe",color.getString("describe"));
                getContentResolver().insert(uriColor,values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
